package com.dgz.springboot18.service.impl;

import com.dgz.springboot18.mapper.AdminMapper;
import com.dgz.springboot18.pojo.AdminInfo;
import com.dgz.springboot18.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 添加一位管理员
     * @param adminInfo
     */
    @Override
    public void addAdmin(AdminInfo adminInfo) {
        adminMapper.save(adminInfo);
    }

    /**
     * 删除一位管理员
     * @param id
     */
    @Override
    public void  delAdmin(String id){
        adminMapper.deleteById(id);
    }

    /**
     * 根据id查询管理员
     * @param id
     * @return
     */
    @Override
    public AdminInfo queryAdmin(String id){
        return adminMapper.findById(id).get();
    }

    /**
     * 根据名称和密码查询管理员
     * @return
     */
    @Override
    public AdminInfo queryByAccount(AdminInfo adminInfo){
        Optional<AdminInfo> result = adminMapper.findOne(Example.of(adminInfo));
        return result.isPresent()?result.get():null;
    }

    /**
     * 根据id修改管理员
     */
    @Override
    public void updateAdmin(AdminInfo adminInfo){
         adminMapper.save(adminInfo);
    }


}
